package info.yaohua.dao;

import info.yaohua.data.DBHelper;
import info.yaohua.dto.Ac02Dto;
import info.yaohua.dto.Ac02Dto;
import info.yaohua.util.UtilHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;

import javax.naming.NamingException;

public class Ac02Dao {
	DBHelper dbHelper = new DBHelper();
	Vector<String> paramList ;
	
	
	public ArrayList<Ac02Dto> findAllByGjz(String gjz) throws NamingException, SQLException
	{
		Connection conn = dbHelper.getConnection();
		String sql = "select * from ac02 where sac001 like '%"
				+gjz
				+"%' or sac002 like '%"
				+gjz
				+"%' or sac003 like '%"
				+gjz
				+"%'";
		PreparedStatement pstmt = null;
		ResultSet rst = null;
		
		Statement st =conn.createStatement();
		rst = st.executeQuery(sql);
		
		ArrayList<Ac02Dto> list = new ArrayList<Ac02Dto>();
		Ac02Dto ac02Dto1 =null;
		while(rst.next())
		{
			ac02Dto1 = new Ac02Dto();
			ac02Dto1.setId(rst.getInt("id"));
			ac02Dto1.setSac001(rst.getString("sac001"));
			ac02Dto1.setSac002(rst.getString("sac002"));
			ac02Dto1.setSac003(rst.getString("sac003"));
			ac02Dto1.setSac004(rst.getDate("sac004"));
			ac02Dto1.setSac005(rst.getDate("sac005"));
			ac02Dto1.setSac006(rst.getString("sac006"));
			ac02Dto1.setSac007(rst.getString("sac007"));
			ac02Dto1.setSac008(rst.getString("sac008"));
			ac02Dto1.setAac001(rst.getString("aac001"));
			
			list.add(ac02Dto1);
			
		}
		try
		{
			rst.close();
			//pstmt.close();
			conn.close();
		}
	catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 返回所有post
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	public ArrayList<Ac02Dto> findAllPost() throws NamingException, SQLException
	{
		Connection conn = dbHelper.getConnection();
		String sql = "select * from ac02 order by sac005 desc";
		PreparedStatement pstmt = null;
		ResultSet rst = null;
		
		Statement st =conn.createStatement();
		rst = st.executeQuery(sql);
		
		ArrayList<Ac02Dto> list = new ArrayList<Ac02Dto>();
		Ac02Dto ac02Dto1 =null;
		while(rst.next())
		{
			ac02Dto1 = new Ac02Dto();
			ac02Dto1.setId(rst.getInt("id"));
			ac02Dto1.setSac001(rst.getString("sac001"));
			ac02Dto1.setSac002(rst.getString("sac002"));
			ac02Dto1.setSac003(rst.getString("sac003"));
			ac02Dto1.setSac004(rst.getDate("sac004"));
			ac02Dto1.setSac005(rst.getDate("sac005"));
			ac02Dto1.setSac006(rst.getString("sac006"));
			ac02Dto1.setSac007(rst.getString("sac007"));
			ac02Dto1.setSac008(rst.getString("sac008"));
			ac02Dto1.setAac001(rst.getString("aac001"));
			
			list.add(ac02Dto1);
			
		}
		try
		{
			rst.close();
			//pstmt.close();
			conn.close();
		}
	catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**通过id删除文章
	 * 
	 * @param ac02Dto
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	public int deleteAc02ById(Ac02Dto ac02Dto) throws NamingException, SQLException
	{
		Connection conn = dbHelper.getConnection();
		String sql = "delete from ac02 where id = ?";
		PreparedStatement pstmt = null;
		ResultSet rst = null;
		
		pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, ac02Dto.getId());
		int success = pstmt.executeUpdate();
		try
		{
			//rst.close();
			pstmt.close();
			conn.close();
		}
	catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return success;
		
	}
	/**
	 * 
	 * @param ac02Dto
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	public int  doUpdateById(Ac02Dto ac02Dto) throws NamingException, SQLException
	{
		Connection conn = dbHelper.getConnection();
		String sql = "update ac02 set sac001='"
				+ac02Dto.getSac001()
				+"',"
				+"sac002='"
				+ac02Dto.getSac002()
				+"',"
				+"sac003='"
				+ac02Dto.getSac003()
				+"',"
				+"sac006='"
				+ac02Dto.getSac006()
				+"',"
				+"sac007='"
				+ac02Dto.getSac007()
				+"',"
				+"sac008='"
				+ac02Dto.getSac008()
				
				+"' where id = ?";
				
		
		PreparedStatement pstmt = null;
		ResultSet rst = null;
		
		pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, ac02Dto.getId());
		int success = pstmt.executeUpdate();
		
		try
		{
		//	rst.close();
			pstmt.close();
			conn.close();
		}
	catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return success;
		
	}
	
	/**
	 * 根据作者查询文章
	 * @param ac02Dto
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	public ArrayList<Ac02Dto> findAc02ByAac001(Ac02Dto ac02Dto) throws NamingException, SQLException
	{
		 Connection conn = dbHelper.getConnection();
			String tableName = "ac02";
			paramList = new Vector<String>();
			paramList.add("aac001");
			String sql = UtilHelper.selectSqlCreate(tableName, paramList);
			
			PreparedStatement pstmt = null;
			ResultSet rst = null;
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, ac02Dto.getAac001());
			rst = pstmt.executeQuery();
			
			ArrayList<Ac02Dto> list = new ArrayList<Ac02Dto>();
			Ac02Dto ac02Dto1 =null;
			while(rst.next())
			{
				ac02Dto1 = new Ac02Dto();
				ac02Dto1.setId(rst.getInt("id"));
				ac02Dto1.setSac001(rst.getString("sac001"));
				ac02Dto1.setSac002(rst.getString("sac002"));
				ac02Dto1.setSac003(rst.getString("sac003"));
				ac02Dto1.setSac004(rst.getDate("sac004"));
				ac02Dto1.setSac005(rst.getDate("sac005"));
				ac02Dto1.setSac006(rst.getString("sac006"));
				ac02Dto1.setSac007(rst.getString("sac007"));
				ac02Dto1.setSac008(rst.getString("sac008"));
				ac02Dto1.setAac001(rst.getString("aac001"));
				
				list.add(ac02Dto1);
				
			}
			
			try
			{
				rst.close();
				pstmt.close();
				conn.close();
			}
		catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return list;
		
	}
	/**
	 * 通过ID查询ac02
	 * @param ac02Dto
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	public Ac02Dto findAc02ById(Ac02Dto ac02Dto) throws NamingException, SQLException
	{
		 Connection conn = dbHelper.getConnection();
			String tableName = "ac02";
			paramList = new Vector<String>();
			paramList.add("id");
			String sql = UtilHelper.selectSqlCreate(tableName, paramList);
			
			PreparedStatement pstmt = null;
			ResultSet rst = null;
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, ac02Dto.getId());
			rst = pstmt.executeQuery();
			
			Ac02Dto ac02Dto1 =null;
			if(rst.next())
			{
				ac02Dto1 = new Ac02Dto();
				ac02Dto1.setId(rst.getInt("id"));
				ac02Dto1.setSac001(rst.getString("sac001"));
				ac02Dto1.setSac002(rst.getString("sac002"));
				ac02Dto1.setSac003(rst.getString("sac003"));
				ac02Dto1.setSac004(rst.getDate("sac004"));
				ac02Dto1.setSac005(rst.getDate("sac005"));
				ac02Dto1.setSac006(rst.getString("sac006"));
				ac02Dto1.setSac007(rst.getString("sac007"));
				ac02Dto1.setSac008(rst.getString("sac008"));
				
				
			}
			try
			{
				rst.close();
				pstmt.close();
				conn.close();
			}
		catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return ac02Dto1;
		
	}
	/**
	 * 保存Ac02 ,返回或得的id值
	 * @param ac02Dto
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	public int doStore(Ac02Dto ac02Dto) throws NamingException, SQLException
	{
		 Connection conn = dbHelper.getConnection();
		 String sql = "insert into ac02 values(null,?,?,?,?,?,?,?,?,?)";
		 PreparedStatement pstmt = null;
			
			
		 pstmt = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS );
		 pstmt.setString(1, ac02Dto.getSac001());
		 pstmt.setString(2, ac02Dto.getSac002());
		 pstmt.setString(3, ac02Dto.getSac003());
		 pstmt.setString(4, ac02Dto.getAac001());
		 pstmt.setDate(5, ac02Dto.getSac004());
		 pstmt.setDate(6, ac02Dto.getSac005());
		 pstmt.setString(7, ac02Dto.getSac006());
		 pstmt.setString(8, ac02Dto.getSac007());
		 pstmt.setString(9, ac02Dto.getSac008());
		 
		 int success = pstmt.executeUpdate();
		 ResultSet rs = pstmt.getGeneratedKeys();
		 int id = -1;
		 if(rs.next())
		 {
			 id = rs.getInt(1);
		 }
		 try
			{
				//rst.close();
				pstmt.close();
				conn.close();
			}
		catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return id;
		
	}
}
